package com.gzc.dao.imp;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.gzc.dao.IDepartmentDao;
import com.gzc.model.Department;

//从2.0版本开始，Spring引入了构造型（stereotype）注解的概念
//以及将@Repository注解作为数据访问代码的标记的方法。
@Repository("departmentDao")
@SuppressWarnings("unchecked")
public class DepartmentDao extends BaseDao<Department> implements
		IDepartmentDao {
	
//	@Override
//	public void add(Department d) {
//		System.out.println(d.getName());
//		super.add(d);
//	}

	@Override
	public List<Integer> listAllExistIds(int depId){
		String hql = "select ds.scopeDep.id from DepScope ds where ds.depId=?";
		return (List)this.listByObj(hql,depId);
	}

	@Override
	public List<Department> listSendDepByUser(int userId) {
		String sql = "select dep.id, dep.name from " +
				"t_user user left join t_dep_scope ds on(ds.dep_id=user.dep_id) left join" +
				" t_dep dep on(dep.id=ds.s_dep_id) where user.id=?";
		return this.getSession().createSQLQuery(sql)
			.setResultTransformer(Transformers.aliasToBean(Department.class))
			.setParameter(0, userId).list();
	}

	@Override
	public List<Department> listByIds(Integer[] depIds) {
		Query q = this.getSession().createQuery("select new Department(d.id,d.name) from Department d where d.id in (:ids)");
		q.setParameterList("ids", depIds);
		return q.list();
	}
	
}
